package com.example.springbootexamples.sort;

import com.alibaba.fastjson.JSON;
import org.junit.Test;

import java.util.*;

class Student {
    private String name;
    private Date birth;
    private Integer score;
    private Double ratio;

    public Double getRatio() {
        return ratio;
    }

    public void setRatio(Double ratio) {
        this.ratio = ratio;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    public Integer getScore() {
        return score;
    }

    public void setScore(Integer score) {
        this.score = score;
    }
}
public class ListSort {


    @Test
    public void t1(){

        List<Student> list=new ArrayList<>();

        Random random=new Random();
        for(int i=0;i<10000;i++){
            Student student = new Student();
            student.setName("i"+i);
            student.setBirth(new Date());
            student.setScore(100);
            student.setRatio(random.nextDouble());
            //student.setRatio(0.1d);
            list.add(student);
        }

        /*{
            Student student = new Student();
            student.setName("aa");
            student.setBirth(new Date());
            student.setScore(100);
            list.add(student);
        }
        {
            Student student = new Student();
            student.setName("e");
            student.setBirth(new Date());
            student.setScore(50);
            list.add(student);
        }
        {
            Student student = new Student();
            student.setName("d");
            student.setBirth(new Date());
            student.setScore(50);
            list.add(student);
        }
        {
            Student student = new Student();
            student.setName("b");
            student.setBirth(new Date());
            student.setScore(50);
            list.add(student);
        }

        {
            Student student = new Student();
            student.setName("c");
            student.setBirth(new Date());
            student.setScore(60);
            list.add(student);
        }*/

        list.sort(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {

                Double d1=o1.getRatio();
                Double d2=o2.getRatio();
                if(d1<=d2){
                    return 0;
                }
                if(d1>d2){
                    return 1;
                }


                /*Integer i1=o1.getScore();
                Integer i2=o2.getScore();
                if(i1<=i2){
                    return -1;
                }else if(i1>i2) {
                    return 1;
                }*/
                return 0;
            }
        });

        System.out.println(JSON.toJSONString(list));

        for(Student student:list){
            //System.out.println(student);
            System.out.println(student.getName()+"\t"+student.getScore());
        }

    }





}
